function throttle(fn, interval, options = { leading: true, trailing: false }) {
  // 1.记录上一次的开始时间
  const { leading, trailing } = options;
  let lastTime = 0;
  let timer = null;
  // 2.事件触发真正运行的函数
  const _throttle = function(...args) {
    // 2.1 获取当前事件触发的时间
    const nowTime = new Date().getTime();
    if(!lastTime && !leading) lastTime = nowTime;

    // 2.2 计算本次触发时间与上次触发时间的间隔
    const remainTime = interval - (nowTime - lastTime)
    if(remainTime <= 0) {
      if(timer) {
        clearTimeout(timer);
        timer = null;
      }
      // 2.3真正触发函数
      fn.apply(this, args);
      // 2.4保留上次的触发时间
      lastTime = nowTime;
      return;
    }

    if(trailing && !timer) {
      timer = setTimeout(() => {
        timer = null;
        lastTime = !leading ? 0 : new Date().getTime();
        fn.apply(this, args);
      }, remainTime)
    }

  }

  _throttle.cancel = function() {
    if(timer) clearTimeout(timer);
    timer = null;
    lastTime = 0;
  }

  return _throttle;
}
